package com.bit.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bit.blog.common.constant.Constants;
import com.bit.blog.common.exception.BlogException;
import com.bit.blog.common.util.BeanTransUtils;
import com.bit.blog.mapper.BlogInfoMapper;
import com.bit.blog.pojo.dataobject.BlogInfo;
import com.bit.blog.pojo.request.AddBlogRequest;
import com.bit.blog.pojo.request.UpdateBlogRequest;
import com.bit.blog.pojo.response.BlogInfoResponse;
import com.bit.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;
    @Override
    //遍历 blogInfos 列表中的每一个 BlogInfo 对象，
    // 使用 BeanTransUtils.trans() 方法将其转换为对应的 BlogInfoResponse 对象，
    // 然后将所有转换后的 BlogInfoResponse 对象收集到一个新的列表 blogInfoResponses 中。
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.BLOG_NORMAL);
        List<BlogInfo> blogInfos=blogInfoMapper.selectList(queryWrapper);

        //将BlogInfo对象转化成BlogInfoService对象进行的相关的转化;
        List<BlogInfoResponse> blogInfoResponses= blogInfos.stream()
                .map(blogInfo -> BeanTransUtils.trans(blogInfo))
                .collect(Collectors.toList());
        return blogInfoResponses;
    }

    //根据传入的博客 ID，查询数据库中 “未删除” 的博客详情，并将数据库实体类（BlogInfo）转换为前端响应类（BlogInfoResponse）返回。
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        return BeanTransUtils.trans(getBlogInfo(blogId));
    }
    @Override
    public BlogInfo getBlogInfo(Integer blogId){
        QueryWrapper<BlogInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,Constants.BLOG_NORMAL)
                .eq(BlogInfo::getId,blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest,blogInfo);
        try {
            Integer result=blogInfoMapper.insert(blogInfo);
            if(result==1){
                return true;
            }
            return false;
        }catch (Exception e){
            log.info("博客插入失败，e:{}",e);
            throw  new BlogException("内部错误，请联系管理员");
        }
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateRequest) {
        BlogInfo blogInfo=BeanTransUtils.trans(updateRequest);
        try{
            Integer result=blogInfoMapper.updateById(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("博客更新失败，e:{}",e);
            throw new BlogException("内部错误，请联系管理员");
        }
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.BLOG_DELETE);
        try{
            Integer result=blogInfoMapper.updateById(blogInfo);
            return result==1;
        }catch (Exception e){
            log.info("博客删除失败，e:{}",e);
            throw new BlogException("内部错误，请联系管理员");
        }
    }
}
